<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="订单号" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入订单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="发货单号" prop="relateBill1">
        <el-input v-model="queryParams.relateBill1" placeholder="请输入发货单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户单号" prop="relateBill2">
        <el-input v-model="queryParams.relateBill2" placeholder="请输入客户单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="success" icon="el-icon-more" size="mini" @click="moreFilterQuery">更多查询</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-upload2" size="mini" :disabled="multiple" @click="handleSign">自提签收</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ledList" @selection-change="handleSelectionChange">
      <el-table-column width="55" type="selection" align="center" />
      <el-table-column width='100' label="出发机构" align="center" prop="fromOrganizationName" />
      <el-table-column width='150' label="编号" align="center" prop="code" fixed />
      <el-table-column width="100" label="订单日期" align="center" prop="time">
        <template #default="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column width='140' label="发货单号" align="center" prop="relateBill1" />
      <el-table-column width='140' label="客户单号" align="center" prop="relateBill2" />
      <el-table-column label="订单状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="dict.type.OrderStatus" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column width='180' label="客户名称" align="center" prop="customerName" />
      <el-table-column width='100' label="是否超期" align="center" prop="customerIsExceed">
        <template #default="scope">
          <dict-tag :options="dict.type.CommIsNot" :value="scope.row.customerIsExceed" />
        </template>
      </el-table-column>
      <el-table-column width='120' label="是否回单" align="center" prop="isBack">
        <template #default="scope">
          <dict-tag :options="dict.type.CommIsNot" :value="scope.row.isBack" />
          <div v-if="scope.row.isBack === 1">回单份数: {{ scope.row.backNumber }}</div>
        </template>
      </el-table-column>
      <el-table-column width='120' label="运输性质" align="center" prop="transportPro">
        <template #default="scope">
          <dict-tag :options="dict.type.TransportPro" :value="scope.row.transportPro" />
        </template>
      </el-table-column>
      <el-table-column width='80' label="名称" align="center" prop="productName" />
      <el-table-column width='80' label="件数(件)" align="center" prop="number" />
      <el-table-column width='80' label="体积(平)" align="center" prop="volume" />
      <el-table-column width='80' label="重量(kg)" align="center" prop="weight" />
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    <el-dialog :close-on-click-modal="false" title="订单追踪" :visible.sync="orderTrackVisible" width="700px" append-to-body>
      <div class="block" style="max-height: 600px; overflow-y: scroll;">
        <el-timeline>
          <el-timeline-item v-for="(item, index) in orderTrackList" :key="index" :timestamp="item.time" placement="top">
            <el-card>
              <h4>{{ item.event }}</h4>
              <p>{{ item.createName }} 提交于 {{ item.time }}</p>
              <div v-if="item.imgUrls">
                <el-image v-for="(img, ind) in item.imgUrls" :key="ind" style="width: 100px; height: 100px;margin: 10px 10px;" :src="img" :preview-src-list="item.imgUrls" />
              </div>
            </el-card>
          </el-timeline-item>
        </el-timeline>
      </div>
    </el-dialog>

    <more-query ref="moreQuery" :query-params="queryParams" @ok="handleQuery"></more-query>

    <el-dialog :close-on-click-modal="false" title="自提签收" :visible.sync="selfPickSignVisible" width="400px" append-to-body>
      <el-form :model="selfPickSignForm" ref="selfPickSignForm" :rules="selfPickSignRules" size="small" label-width="80px">
        <el-form-item label="签收人" prop="qsPerson">
          <el-input v-model="selfPickSignForm.qsPerson" placeholder="请输入签收人" clearable />
        </el-form-item>
        <el-form-item label="签收时间" prop="qsTime">
          <el-date-picker v-model="selfPickSignForm.qsTime" type="datetime" placeholder="请选择签收时间" clearable />
        </el-form-item>
        <el-form-item label="备注" prop="description">
          <el-input v-model="selfPickSignForm.description" type="textarea" placeholder="请输入备注" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSignSubmit">确 定</el-button>
        <el-button @click="selfPickSignVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInventory, getLed, setSelfPickSign } from "@/api/order/led";
import { listOrderTrackByOrderId } from "@/api/order/orderTrack";
import MoreQuery from "@/views/order/led/component/moreQuery.vue";

export default {
  name: "InventoryTable",
  components: { MoreQuery },
  dicts: ['OrderStatus', 'CommIsNot', 'TransportPro', 'HandoverType'],
  props: {
    inventoryType: {
      type: String,
      default: 'OPERATIONAL'
    }
  },
  watch: {
    inventoryType: {
      handler: function (val, oldVal) {
        this.getList();
      },
      immediate: true
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // ledId
      ledId: null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分段订单表格数据
      ledList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        inventoryType: 'OPERATIONAL',
        code: null,
        createName: null,
        description: null,
        modifyName: null,
        modifyTime: null,
        name: null,
        status: null,
        backNumber: null,
        customerIsExceed: null,
        customerName: null,
        customerType: null,
        factArriveTime: null,
        factLeaveTime: null,
        feeType: null,
        handoverType: null,
        isAbnormal: null,
        isBack: null,
        number: null,
        actNumber: null,
        orderMileage: null,
        planArriveTime: null,
        planLeaveTime: null,
        relateBill1: null,
        relateBill2: null,
        relateBill3: null,
        relateBill4: null,
        relateBill5: null,
        salePerson: null,
        shipmentMethod: null,
        time: null,
        transportPro: null,
        value: null,
        volume: null,
        weight: null,
        jcCustomerId: null,
        jcEndZoneId: null,
        jcOrderId: null,
        jcOrganizationId: null,
        jcPrescoId: null,
        jcDispatchId: null,
        jcStartZoneId: null,
        jcToOrganizationId: null,
        routerOrgIds: null,
        jcZoneStoreroomId: null,
        jcFormOrganizationId: null,
        type: null,
        agent: null,
        qsTime: null,
        qsPerson: null,
        qsImgs: null,
        jzWeight: null,
        arrivePay: null,
        jcShipmentId: null,
        // 计划发运时间
        daterangePlanLeaveTime: [],
        // 运单ID时间范围
        daterangeCreateTime: [],
        // 运单ID时间范围
        daterangeModifyTime: [],
        // 运单ID时间范围
        daterangeFactArriveTime: [],
        // 运单ID时间范围
        daterangeFactLeaveTime: [],
        // 运单ID时间范围
        daterangeTime: [],
        // 运单ID时间范围
        daterangeQsTime: [],
      },
      orderTrackList: [],
      orderTrackVisible: false,
      selfPickSignVisible: false,
      selfPickSignForm: {
        ledIds: null,
        qsPerson: null,
        qsTime: null,
        description: null,
      },
      selfPickSignRules: {
        qsPerson: [
          { required: true, message: "签收人不能为空", trigger: "blur" }
        ],
        qsTime: [
          { required: true, message: "签收时间不能为空", trigger: "blur" }
        ],
      },

    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.getList();
  },
  methods: {
    /** 查询分段订单列表 */
    getList() {
      this.ledList = [];
      this.loading = true;
      this.queryParams.inventoryType = this.inventoryType;
      this.queryParams.params = {};
      if (null != this.queryParams.daterangePlanLeaveTime && '' !== this.queryParams.daterangePlanLeaveTime) {
        this.queryParams.params["beginPlanLeaveTime"] = this.queryParams.daterangePlanLeaveTime[0];
        this.queryParams.params["endPlanLeaveTime"] = this.queryParams.daterangePlanLeaveTime[1];
      }
      if (null != this.queryParams.daterangeCreateTime && '' !== this.queryParams.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.queryParams.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.queryParams.daterangeCreateTime[1];
      }
      if (null != this.queryParams.daterangeModifyTime && '' !== this.queryParams.daterangeModifyTime) {
        this.queryParams.params["beginModifyTime"] = this.queryParams.daterangeModifyTime[0];
        this.queryParams.params["endModifyTime"] = this.queryParams.daterangeModifyTime[1];
      }
      if (null != this.queryParams.daterangeFactArriveTime && '' !== this.queryParams.daterangeFactArriveTime) {
        this.queryParams.params["beginFactArriveTime"] = this.queryParams.daterangeFactArriveTime[0];
        this.queryParams.params["endFactArriveTime"] = this.queryParams.daterangeFactArriveTime[1];
      }
      if (null != this.queryParams.daterangeFactLeaveTime && '' !== this.queryParams.daterangeFactLeaveTime) {
        this.queryParams.params["beginFactLeaveTime"] = this.queryParams.daterangeFactLeaveTime[0];
        this.queryParams.params["endFactLeaveTime"] = this.queryParams.daterangeFactLeaveTime[1];
      }
      if (null != this.queryParams.daterangeTime && '' !== this.queryParams.daterangeTime) {
        this.queryParams.params["beginTime"] = this.queryParams.daterangeTime[0];
        this.queryParams.params["endTime"] = this.queryParams.daterangeTime[1];
      }
      if (null != this.queryParams.daterangeQsTime && '' !== this.queryParams.daterangeQsTime) {
        this.queryParams.params["beginQsTime"] = this.queryParams.daterangeQsTime[0];
        this.queryParams.params["endQsTime"] = this.queryParams.daterangeQsTime[1];
      }
      listInventory(this.queryParams).then(response => {
        this.ledList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.daterangePlanLeaveTime = [];
      this.queryParams.daterangeCreateTime = [];
      this.queryParams.daterangeModifyTime = [];
      this.queryParams.daterangeFactArriveTime = [];
      this.queryParams.daterangeFactLeaveTime = [];
      this.queryParams.daterangeTime = [];
      this.queryParams.daterangeQsTime = [];
      this.resetForm("queryForm");
      this.$refs.moreQuery.resetQuery();
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    openTrack(row) {
      this.orderTrackVisible = true
      this.orderTrackList = []
      /** 查询订单追踪列表 */
      this.loading = true;
      listOrderTrackByOrderId(row.orderId).then(response => {
        this.loading = false;
        this.orderTrackList = response.data;
        // 处理图片地址
        if (this.orderTrackList.length > 0) {
          this.orderTrackList.forEach(item => {
            if (item.imgs) {
              // 每个图片地址都加上baseUrl
              item.imgUrls = item.imgs.split(',').map(d => process.env.VUE_APP_BASE_API + d)
            }
          })
        }
      });
    },
    moreFilterQuery() {
      this.$refs.moreQuery.moreFilterQuery();
    },
    // 自提签收
    handleSign() {
      this.selfPickSignVisible = true;
      this.selfPickSignForm.ledIds = this.ids;
    },
    handleSignSubmit(id) {
      this.$refs["selfPickSignForm"].validate(valid => {
        if (!valid) {
          return false;
        }
        this.selfPickSignForm.ledId = id;
        setSelfPickSign(this.selfPickSignForm).then(response => {
          this.$modal.msgSuccess("签收成功");
          this.selfPickSignVisible = false;
          this.resetSignForm();
          this.getList();
        });
      });
    },
    resetSignForm() {
      this.selfPickSignForm = {
        ledId: null,
        qsPerson: null,
        qsTime: null,
        description: null,
      };
      this.resetForm("selfPickSignForm");
    },
  }
};
</script>
